package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.Page;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.*;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService  {

    @Autowired
    private UserMapper userMapper ;

    @Autowired
    private OrdersMapper ordersMapper ;

    @Autowired
    private OrdersDetailMapper ordersDetailMapper ;

    @Autowired
    private AddressMapper addressMapper ;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper ;

    //添加订单
    @Override
    @Transactional
    public Result add(Orders orders) {

        //获取订单id，因为要考虑id的安全性，所有就是需要自己创建订单id
        long orderId  = IdUtil.getSnowflake().nextId();

        //获取总金额,给初始值，通过购物车计算
        BigDecimal bigDecimal = new BigDecimal(0);

        //根据用户id来获取user
        User user = userMapper.findById(orders.getUserId());

        //根据地址id查询地址
        Address address = addressMapper.findById(orders.getAddressBookId());

        //现根据用户id来查询购物车中的数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByUserId(orders.getUserId());
        //然后因为属性相同，所有就是复制给订单细节
        //先创建订单细节列表
        List<OrderDetail> orderDetails = new ArrayList<>();
        //循环这个购物车列表
        for (ShoppingCart shoppingCart : shoppingCartList) {
            //创建一个新的订单细节对象
            OrderDetail orderDetail = new OrderDetail();
            //然后就是将购物车实体类赋值给订单细节对象
            BeanUtils.copyProperties(shoppingCart , orderDetail );
            //将订单的id设置为空
            orderDetail.setId(null);
            //为订单id设置属性
            orderDetail.setOrderId(orderId);
            //将订单细节对象保存进集合中
            orderDetails.add(orderDetail);

            //统计多少钱
            bigDecimal = bigDecimal.add(orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber())));

        }

        //然后调用订单细节mapper来保存订单细节集合
        ordersDetailMapper.save(orderDetails);
        //设置主键id
        orders.setId(orderId);
        //设置订单号
        orders.setNumber(String.valueOf(orderId));
        //设置状态
        orders.setStatus(1);
        //下单用户的id
        orders.setUserId(orders.getUserId());
        //地址已经获取
        //下单时间
        orders.setOrderTime(LocalDateTime.now());
        //付款时间
        orders.setCheckoutTime(LocalDateTime.now());
        //支付方式
        orders.setPayMethod(1);
        //实收金额
        orders.setAmount(bigDecimal);
        //用户名
        orders.setUsername(user.getName());
        //手机号
        orders.setPhone(user.getPhone());
        //地址
        orders.setAddress(address.getDetail());
        //收货人
        orders.setConsignee(address.getConsignee());

        //封装好数据就添加进订单表
        ordersMapper.save(orders);

        //删除购物车的数据
        shoppingCartMapper.deleteByUserId(orders.getUserId());
        //返回值
        return Result.success(null);
    }


    //获取所有订单信息
    @Override
    public Result page(Integer page, Integer pageSize, String number) {
        //开启pagehelper
        PageHelper.startPage( page , pageSize );
        //查询所有的订单明细
        List<Orders> ordersList = ordersMapper.page(number);
        //放入pageInfo中
        PageInfo<Orders> ordersPageInfo = new PageInfo<>(ordersList);
        //获取拦截后的值
        List<Orders> list = ordersPageInfo.getList();
        //然后就创建一个page
        Page page1 = new Page();
        //为page赋值
        page1.setTotal(ordersPageInfo.getTotal());
        //为page赋值
        page1.setRecords(list);
        return Result.success(page1);

    }

    //查看该用户的订单
    @Override
    public Page findByUserId(Integer page, Integer pageSize, Long user) {

        //开启页面
        PageHelper.startPage(page , pageSize);

        //查看订单数据
        List<Orders> ordersList =  ordersMapper.findByUserId(user);

        for (Orders orders : ordersList) {
            //获取订单id
            Long id = orders.getId();
            //根据订单id来查询订单细节集合
            List<OrderDetail> orderDetailList =  ordersDetailMapper.findByOrderID(id);
            //获取到之后就赋值给order的属性
            orders.setOrderDetails(orderDetailList);
        }

        PageInfo<Orders> pageInfo = new PageInfo(ordersList);

        List<Orders> list = pageInfo.getList();

        Page page1 = new Page();

        page1.setRecords(list);

        page1.setTotal(pageInfo.getTotal());

        return page1 ;
    }


}
